@router:
from odoo import http
from odoo.http import request
class library_contorller(http.Controller):
@http.route('/my_library/books',method=['GET'], type='http', auth='none')
def books(self):
books = request.env['library.book'].sudo().search([])
html_result = '<html><body><ul>'
for book in books:
html_result += "<li> %s </li>" % book.name
html_result += '</ul></body></html>'
return html_result
在@route('/my_library/books', type='http', auth='none')
當中'/my_library/books'
就是我們的網址後綴,還有我們使用了type='http'
,透過request.env[]
將library.book
中
所有recordset給抓出來並透過for來將他們列出來 ; auth='none'
代表任何人都可以訪問.../my_library/books
這個網址。